package com.googlecode.garbagecan.test.socket.sample7;

import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool;

public class MyClientUtil {

	private ObjectPool pool = null;

	public MyClientUtil(String host, int port, boolean enableSSL) {
		PoolableObjectFactory factory = new MyClientPoolableObjectFactory(host, port, enableSSL);
		GenericObjectPool.Config config = new GenericObjectPool.Config();
		config.lifo = false;
		config.maxActive = 100;
		config.maxIdle = 10;
		config.minIdle = 10;
		config.maxWait = 10 * 1000;

		pool = new GenericObjectPool(factory, config);
	}

	public void execute() throws Exception {
		MyClient myClient = null;

		try {
			myClient = (MyClient)pool.borrowObject();
			myClient.execute();
		} finally {
			try {
				if (myClient != null) {
					pool.returnObject(myClient);
				}
			} catch (Exception e) {
				// ignore me
			}
		}
	}
}
